package com.rightsidetech.http.controller.userApp.mall;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.response.gift.ActGiftUserAppResp;
import com.rightsidetech.activity.service.ActGiftService;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.mall.dto.request.userApp.CommodityAppDetailReq;
import com.rightsidetech.mall.dto.request.userApp.CommodityAppPageReq;
import com.rightsidetech.mall.dto.request.userApp.OptCommodityReq;
import com.rightsidetech.mall.dto.response.TypePageResp;
import com.rightsidetech.mall.dto.response.userApp.CommodityAppDetailResp;
import com.rightsidetech.mall.dto.response.userApp.CommodityAppPageResp;
import com.rightsidetech.mall.dto.response.userApp.OptCommodityOneResp;
import com.rightsidetech.mall.dto.response.userApp.OptCommodityResp;
import com.rightsidetech.mall.entity.MallGiftCommodity;
import com.rightsidetech.mall.service.MallGiftCommodityService;
import com.rightsidetech.mall.service.MallTypeService;
import com.rightsidetech.mall.service.MallUserAppCommodityService;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.service.SysStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/userApp/mall/")
@ShenyuSpringMvcClient(path = "/userApp/mall/**")
@Api(tags = "用户商城商品相关")
public class MallCommodityAppController {

    @Resource
    private MallTypeService mallTypeService;

    @Resource
    private MallUserAppCommodityService mallUserAppCommodityService;
    @Resource
    private ActGiftService actGiftService;
    @Resource
    private MallGiftCommodityService mallGiftCommodityService;

    @Resource
    private SysStoreInfoService sysStoreInfoService;

    @Log
    @GetMapping("/type_all")
    @ApiOperation("获取分类列表")
    public ResponseData<List<TypePageResp>> typeAll(Long storeId){
        if(ObjectUtil.isEmpty(storeId)){
            return ResponseData.success(CollUtil.newArrayList());
        }
        SysStoreInfo store = sysStoreInfoService.selectByPrimaryKey(storeId);
        if(ObjectUtil.isEmpty(store)){
            return ResponseData.fail("未找到门店");
        }
        if(store.getStatus() == -1){
            return ResponseData.fail("当前门店未营业");
        }
        return mallTypeService.getAppTypeList(store.getSysAgentInfoId());
    }

    @Log
    @GetMapping("/page")
    @ApiOperation("分页获取商品列表")
    public ResponseData<PageInfo<CommodityAppPageResp>> pageCommodity(CommodityAppPageReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreInfo store = sysStoreInfoService.selectByPrimaryKey(req.getStoreId());
        if(ObjectUtil.isEmpty(store)){
            return ResponseData.fail("未找到门店");
        }
        if(store.getStatus() == -1){
            return ResponseData.fail("当前门店未营业");
        }
        return mallUserAppCommodityService.page(req,store.getSysAgentInfoId());
    }

    @Log
    @GetMapping("/detail")
    @ApiOperation("获取商品详情")
    public ResponseData<CommodityAppDetailResp> getDetail(CommodityAppDetailReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreInfo store = sysStoreInfoService.selectByPrimaryKey(req.getStoreId());
        if(ObjectUtil.isEmpty(store)){
            return ResponseData.fail("未找到门店");
        }
        if(store.getStatus() == -1){
            return ResponseData.fail("当前门店未营业");
        }
        ResponseData<CommodityAppDetailResp> result = mallUserAppCommodityService.findDetailById(req.getId(), store.getSysAgentInfoId());
        if(result.getCodeId() == 1){
            CommodityAppDetailResp data = result.getResData();
            data.setIsGift(actGiftService.isGiftByTypeId(data.getTypeId(),req.getStoreId()));
        }
        return result;
    }

    @Log
    @GetMapping("/one_opt")
    @ApiOperation("第一次进入选择商品页面")
    public ResponseData<OptCommodityOneResp> oneGetCommodity(Long id){
        return mallUserAppCommodityService.oneCommodity(id);
    }

    @Log
    @PostMapping("/opt_commodity")
    @ApiOperation("选择商品")
    public ResponseData<OptCommodityResp> optCommodity(@RequestBody OptCommodityReq req){
        return mallUserAppCommodityService.optCommodity(req);
    }



    @Log
    @GetMapping("/get_gift")
    @ApiOperation("根据商品分类id获取换购商品")
    public ResponseData<List<ActGiftUserAppResp>> getGiftCommodity(@RequestParam("typeId") Long typeId,@RequestParam("storeId") Long storeId){
        if(ObjectUtil.isEmpty(typeId) || ObjectUtil.isEmpty(storeId)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"分类/门店 id不可为空");
        }
        List<ActGiftUserAppResp> giftAndnum = actGiftService.selectGiftIdAndNum(typeId,storeId);
        if(CollUtil.isNotEmpty(giftAndnum)){
            List<Long> ids = giftAndnum.stream().map(ActGiftUserAppResp::getId).collect(Collectors.toList());
            List<MallGiftCommodity> commodityList = mallGiftCommodityService.selectByIds(ids);
            for (MallGiftCommodity commodity : commodityList) {
                for (ActGiftUserAppResp resp : giftAndnum) {
                    if(resp.getId().equals(commodity.getId())){
                        resp.setCommodityName(commodity.getCommodityName());
                        resp.setUnderlinedPrice(commodity.getUnderlinedPrice());
                        resp.setPrice(commodity.getPrice());
                        resp.setSpecs(commodity.getSpecs());
                        resp.setPicture(commodity.getCover());
                        resp.setGiftType(commodity.getGiftType());
                    }
                }
            }
        }
        return ResponseData.success(giftAndnum);
    }
}
